import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;


public class defaultStyle extends Style {

	private Image bj;
	//private GridLayout myLayout = new GridLayout(8, 8, 0, 0);
	private Toolkit tool;
	private Player myPlayer;
	private int team;
	private int boardSize = 8;
	chessButton[][] cb = new chessButton[8][8];

	public defaultStyle() {
		this.setLayout(null);
		this.setSize(800, 800);
		tool = getToolkit();
		//String filename="image/Board.jpg";
		//Toolkit toolkit = Toolkit.getDefaultToolkit();
		//URL url = ClassLoader.getSystemResource("image/Board.jpg");
		//this.bj = 
		bj =tool.getImage(getClass().getResource("Board.jpg"));
		//setBoard();
	}

	public void setBoard() {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				this.cb[i][j] = new chessButton(this,((this.boardSize-1)*(this.myPlayer.getTeam())+i*(1-this.myPlayer.getTeam()*2)) ,
						((this.boardSize - 1)*(1-this.myPlayer.getTeam()) + j*(this.myPlayer.getTeam()*2-1)) ,new defaultAdrs(i,j));

				this.cb[i][j].noChess();
				this.add(this.cb[i][j]);
				//System.out.println(this.myPlayer.getTeam());
			}
		}
		//this.drawChess(0, 1, null);
		
	}

	public void drawBoard(ROBoard b) {
		// TODO Auto-generated method stub
		if (this.cb[0][0] == null) {
			this.setBoard();
		}
		for (int x = 0; x < 8; x++) {
			for (int y = 0; y < 8; y++) {
				if (b.getChess(new defaultAdrs(x, y)) == null) {
					this.cb[x][y].noChess();
				} else {
					//System.out.println("c");
					Chess temp = b.getChess(new defaultAdrs(x, y));
					this.cb[x][y].setChess(temp.getPost(), temp.getTeam());
					if (this.myPlayer.getTeam() == temp.getTeam()) {
						this.cb[x][y].canMove();
					} else {
						this.cb[x][y].cantMove();
					}
				}

			}
		}
		

	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(bj, 0, 0, this);
	}

	public void drawMovement(AdrsList al) {
		// TODO Auto-generated method stub
		for(int i=0;i<al.getPathNum();i++){
			while(al.getAdrsNum(i)>0){
				this.cb[al.lookFirstAdrs(i).getAdrs(0)][al.lookFirstAdrs(i).getAdrs(1)].setMovements();
				al.getFirstAdrs(i);
			}
		}
	}

	@Override
	public void drawChess(int post, int team,Adrs a) {
		this.cb[0][2].setChess(post,team);
		if(team==this.team){
			this.cb[0][2].canMove();
		}else{
			this.cb[0][2].cantMove();
		}
		// TODO Auto-generated method stu
	}

	@Override
	public void call(Adrs adrs) {
		// TODO Auto-generated method stub
		this.myPlayer.call(adrs);

	}

	public void setBoardSize(int s) {
		this.boardSize = s;
	}

	public void setTeam(int t) {
		this.team = t;
	}

	@Override
	public void setPlayer(Player p) {
		this.myPlayer=p;
		this.team=this.myPlayer.getTeam();
		
	}


}
